//
//  ViewController.swift
//  视频悬浮框Swift
//
//  Created by 解楚豪 on 2018/9/10.
//  Copyright © 2018年 xch. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    var btn:UIButton?;
    var videoView:UIView?
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        btn = UIButton.init(type: .custom)
        btn?.frame = CGRect.init(x: 100, y: 100, width: 100, height: 100)
        btn?.setTitle("跳转视频", for: UIControlState.normal)
        btn?.setTitleColor(UIColor.black, for: .normal)
        btn?.layer.borderColor = UIColor.orange.cgColor
        btn?.layer.borderWidth = 2.4
        btn?.layer.cornerRadius = 0.5
        btn?.addTarget(self, action: #selector(videoPlayAction), for: UIControlEvents.touchUpInside)
        self.view.addSubview(btn!)
        
        videoView = UIView.init(frame: .init(x: 100, y: 300, width: 200, height: 150))
        videoView?.backgroundColor = UIColor.orange
        self.view.addSubview(videoView!)
    }
    
    @objc func videoPlayAction() {
        let videoPlay = VideoPlayViewController()
        weak var weakSelf = self

        videoPlay.xchBLock = {()->Void in
            XCHPlayObject.shareInstance().frame = (weakSelf?.videoView?.bounds)!
            XCHPlayObject.shareInstance().supperAddView(supView: (weakSelf?.videoView!)!)
        }
        videoPlay.videoUrl = "http://media.nenghealth.com/upload/20180402/20180402125430_365.mp4"
        self.present(videoPlay, animated: true, completion: nil)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        for touch:AnyObject in touches {
            let t:UITouch = touch as! UITouch
            let point = t.previousLocation(in: self.view)
            if t.view == videoView {
                videoView?.center = point
            }
        }
        
        
    }
}

